using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Collections.Generic;

namespace EM.Repository.utility
{
    public class smtpSendMail
    {
        AppSettingsReader asr = new AppSettingsReader();
        DbHelper Dsrv = new DbHelper();

        private string _mFrom = ""; // MAIL_SERVER_USER
        private string _mDisplayName = "Expense Management System";
        private List<string> _mTo;
        private string _mCc = "";
        private string _mBcc = "";
        private string _mSubject = "";
        private string _mMailBody = "";
        private bool _mIsBodyHtml = true;
        private bool _mResultBool = false;
        private string _mResultString = "Unknown";
        private string _mExceptionMessage = "";
        private string _mMailServer = ""; // MAIL_SERVER_ADDRESS
        private string _mMailUser = ""; // MAIL_SERVER_USER
        private string _mMailUserPassword = ""; // MAIL_SERVER_PASS
        private int _mMailUserPortNo = 0; // MAIL_SERVER_PORT_NUMBER

        public string mFrom
        {
            get { return _mFrom; }
            set { _mFrom = value; }
        }

        public string mDisplayName
        {
            get { return _mDisplayName; }
            set { _mDisplayName = value; }
        }

        public List<string> mTo
        {
            get { return _mTo; }
            set { _mTo = value; }
        }

        public string mCc
        {
            get { return _mCc; }
            set { _mCc = value; }
        }

        public string mBcc
        {
            get { return _mBcc; }
            set { _mBcc = value; }
        }

        public string mSubject
        {
            get { return _mSubject; }
            set { _mSubject = value; }
        }

        public string mMailBody
        {
            get { return _mMailBody; }
            set { _mMailBody = value; }
        }

        public bool mIsBodyHtml
        {
            get { return _mIsBodyHtml; }
            set { _mIsBodyHtml = value; }
        }

        public bool mResultBool
        {
            get { return _mResultBool; }
            set { _mResultBool = value; }
        }

        public string mResultString
        {
            get { return _mResultString; }
            set { _mResultString = value; }
        }

        public string mExceptionMessage
        {
            get { return _mExceptionMessage; }
            set { _mExceptionMessage = value; }
        }

        public string mMailServer
        {
            get { return _mMailServer; }
            set { _mMailServer = value; }
        }

        public string mMailUser
        {
            get { return _mMailUser; }
            set { _mMailUser = value; }
        }

        public string mMailUserPassword
        {
            get { return _mMailUserPassword; }
            set { _mMailUserPassword = value; }
        }

        public int mMailUserPortNo
        {
            get { return _mMailUserPortNo; }
            set { _mMailUserPortNo = value; }
        }


        public string sendMailNow()
        {
            try
            {
                if ((this.mTo.Count == 0 || this.mTo == null) || this.mSubject == "" || this.mMailBody == "")
                {
                    this.mResultBool = false;
                    this.mResultString = "UNSuccessful";
                }
                else
                {
                    this.mDisplayName = Dsrv.GetGlobalDefaultValue("MAIL_DISPLAY_NAME");
                    this.mMailUserPortNo = Convert.ToInt32(Dsrv.GetGlobalDefaultValue("MAIL_SERVER_PORT_NUMBER"));
                    SmtpClient oSmtpClient = new SmtpClient(Dsrv.GetGlobalDefaultValue("MAIL_SERVER_ADDRESS"), mMailUserPortNo);
                    MailMessage oMailMessage = new MailMessage();    
                    MailAddress oMailAddress = new MailAddress(Dsrv.GetGlobalDefaultValue("MAIL_SERVER_USER"), this.mDisplayName);
                    
                    oMailMessage.From = oMailAddress;
                    foreach (string str in this.mTo)
                    {
                        oMailMessage.To.Add(str);
                    }
                    oMailMessage.Subject = this.mSubject;
                    oMailMessage.Body = this.mMailBody;
                    oMailMessage.IsBodyHtml = this.mIsBodyHtml;
                    
                    if (this.mCc != "")
                    {
                        oMailMessage.CC.Add(this.mCc);
                    }
                    if (this.mBcc != "")
                    {
                        oMailMessage.Bcc.Add(this.mBcc);
                    }
                    
                    
                    NetworkCredential crdUser = new NetworkCredential(Dsrv.GetGlobalDefaultValue("MAIL_SERVER_USER"), Dsrv.GetGlobalDefaultValue("MAIL_SERVER_PASS"));
                    oSmtpClient.UseDefaultCredentials = false;
                    oSmtpClient.Credentials = crdUser;
                    if (Dsrv.GetGlobalDefaultValue("MAIL_SERVER_ENABLESSL") == "Y")
                    {
                        oSmtpClient.EnableSsl = true;
                    }
                    
                    oSmtpClient.Send(oMailMessage);
                    this.mResultBool = true;
                    this.mResultString = "Successful";
                }
                return this.mResultString;
            }
            catch (Exception e)
            {
                this.mResultBool = false;
                this.mExceptionMessage = e.ToString();
                this.mResultString = "UNSuccessful \\n Error : " + this.mExceptionMessage;
                return this.mResultString;
            }
        }

        private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
            String token = (string)e.UserState;

            if (e.Cancelled)
            {
                this.mResultString = string.Format("[{0}] Send canceled.", token);
            }
            if (e.Error != null)
            {
                this.mResultString = string.Format("[{0}] {1}", token, e.Error.ToString());
            }
            else
            {
                this.mResultString = "Message sent.";
            }
            this.mResultBool = true;
        }

    }
}
